package handler

import (

	"golang.org/x/net/context"
	scope "git.oschina.net/fanbuchi/xgggh/oauth2/proto/scope"
	"git.oschina.net/fanbuchi/xgggh/oauth2/model"
	"git.oschina.net/fanbuchi/xgggh/oauth2/db"
	"encoding/json"
	"strconv"
)

type Scope struct{}



//micro query com.xgggh.srv.oauth2 ScopeSrv.Create '{"scope":"read","description":"read","isDefault": 1}'
//micro query com.xgggh.srv.oauth2 ScopeSrv.Create '{"scope":"read_write","description":"read_write","isDefault": 0}'


// Call is a single request handler called via client.Call or the generated client code
func (e *Scope) Create(ctx context.Context, req *scope.ScopeCreateReq, rsp *scope.ScopeCreateRsp) error {
	_scope:=model.Scope{Scope:req.Scope,
		Description:req.Description,
		IsDefault:req.IsDefault}
	err:=db.GetDB().Create(&_scope).Error
	return err
}
//micro query com.xgggh.srv.oauth2 ScopeSrv.Query '{"id":245534927428059137}'

func (e *Scope) Query(ctx context.Context, req *scope.ScopeQueryReq, rsp *scope.ScopeQueryRsp) error {
	var _scope model.Scope;
	err:=db.GetDB().First(&_scope, req.Id).Error
	if _scope.ID<=0 {
		return nil
	}

	var deleted int64
	if _scope.DeletedAt!=nil {
		deleted=_scope.DeletedAt.Unix()
	}

	rsp.Data=&scope.ScopeMsg{Id:int64(_scope.ID),
		Scope:_scope.Scope,
		Description:_scope.Description,
		IsDefault:_scope.IsDefault,
		CreatedAt:_scope.CreatedAt.Unix(),
		UpdatedAt:_scope.UpdatedAt.Unix(),
		DeletedAt:deleted,
		}
	return err
}

//micro query com.xgggh.srv.oauth2 AccountSrv.Update '{"id":245064531848396801,"account":{"userName":"lisi","roleId":120,"password":"123456"}}'

func (e *Scope) Update(ctx context.Context, req *scope.ScopeUpdateReq, rsp *scope.ScopeUpdateRsp) error {
	_scope:=req.Scope
	if  _scope==nil{
		return nil
	}
	_scope.Id=req.Id
	err:= db.GetDB().Model(&_scope).Updates(model.Scope{
		Scope:_scope.Scope,
		Description:_scope.Description,
		IsDefault:_scope.IsDefault}).Error
	if err ==nil{
		rsp.Data=_scope
	}
	return err
}
//micro query com.xgggh.srv.oauth2 AccountSrv.Delete '{"id":245064531848396801}'

func (e *Scope) Delete(ctx context.Context, req *scope.ScopeDeleteReq, rsp *scope.ScopeDeleteRsp) error {
	_scope:=model.Scope{}
	_scope.ID=uint(req.Id)
	err:=db.GetDB().Delete(&_scope).Error
	return err
}

//micro query com.xgggh.srv.oauth2 AccountSrv.Search '{"page":1,"per":10}'
//search by userName,metaUserId ...
//micro query com.xgggh.srv.oauth2 AccountSrv.Search '{"page":1,"per":10,"params":{"userName":"15108280907"}}'
func (e *Scope) Search(ctx context.Context, req *scope.ScopeSearchReq, rsp *scope.ScopeSearchRsp) error {
	var _scope [] *model.Scope;
	var __scope [] *scope.ScopeMsg;
	//page
	var page int64=1;
	if req.Page>0 {
		page=req.Page
	}
	offset:=(page-1)*req.PerPage

	//support below data filter

	_default,_:=strconv.ParseInt(req.Params["isDefault"],10,0)

	params:=model.Scope{
		 Scope: req.Params["scope"],
		 Description:req.Params["description"],
		 IsDefault:_default,
	 }
	//db
	err:=db.GetDB().
		Where(params).
		Offset(offset).
		Limit(req.PerPage).
		Find(&_scope).
		Error
	//data
	data,err:=json.Marshal(&_scope)
	json.Unmarshal(data,__scope)
	if err ==nil{
		rsp.Data=__scope
	}
	return  err
}